1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module gdk.ContentProvider;
26 
27 private import gdk.ContentFormats;
28 private import gdk.c.functions;
29 public  import gdk.c.types;
30 private import gio.AsyncResultIF;
31 private import gio.Cancellable;
32 private import gio.OutputStream;
33 private import glib.Bytes;
34 private import glib.ConstructionException;
35 private import glib.ErrorG;
36 private import glib.GException;
37 private import glib.MemorySlice;
38 private import glib.Str;
39 private import gobject.ObjectG;
40 private import gobject.Signals;
41 private import gobject.Value;
42 private import std.algorithm;
43 
44 
45 /**
46  * A `GdkContentProvider` is used to provide content for the clipboard or
47  * for drag-and-drop operations in a number of formats.
48  * 
49  * To create a `GdkContentProvider`, use [ctor@Gdk.ContentProvider.new_for_value]
50  * or [ctor@Gdk.ContentProvider.new_for_bytes].
51  * 
52  * GDK knows how to handle common text and image formats out-of-the-box. See
53  * [class@Gdk.ContentSerializer] and [class@Gdk.ContentDeserializer] if you want
54  * to add support for application-specific data formats.
55  */
56 public class ContentProvider : ObjectG
57 {
58 	/** the main Gtk struct */
59 	protected GdkContentProvider* gdkContentProvider;
60 
61 	/** Get the main Gtk struct */
62 	public GdkContentProvider* getContentProviderStruct(bool transferOwnership = false)
63 	{
64 		if (transferOwnership)
65 			ownedRef = false;
66 		return gdkContentProvider;
67 	}
68 
69 	/** the main Gtk struct as a void* */
70 	protected override void* getStruct()
71 	{
72 		return cast(void*)gdkContentProvider;
73 	}
74 
75 	/**
76 	 * Sets our main struct and passes it to the parent class.
77 	 */
78 	public this (GdkContentProvider* gdkContentProvider, bool ownedRef = false)
79 	{
80 		this.gdkContentProvider = gdkContentProvider;
81 		super(cast(GObject*)gdkContentProvider, ownedRef);
82 	}
83 
84 
85 	/** */
86 	public static GType getType()
87 	{
88 		return gdk_content_provider_get_type();
89 	}
90 
91 	/**
92 	 * Create a content provider that provides the given @bytes as data for
93 	 * the given @mime_type.
94 	 *
95 	 * Params:
96 	 *     mimeType = the mime type
97 	 *     bytes = a `GBytes` with the data for @mime_type
98 	 *
99 	 * Returns: a new `GdkContentProvider`
100 	 *
101 	 * Throws: ConstructionException GTK+ fails to create the object.
102 	 */
103 	public this(string mimeType, Bytes bytes)
104 	{
105 		auto __p = gdk_content_provider_new_for_bytes(Str.toStringz(mimeType), (bytes is null) ? null : bytes.getBytesStruct());
106 
107 		if(__p is null)
108 		{
109 			throw new ConstructionException("null returned by new_for_bytes");
110 		}
111 
112 		this(cast(GdkContentProvider*) __p, true);
113 	}
114 
115 	/**
116 	 * Create a content provider that provides the given @value.
117 	 *
118 	 * Params:
119 	 *     value = a `GValue`
120 	 *
121 	 * Returns: a new `GdkContentProvider`
122 	 *
123 	 * Throws: ConstructionException GTK+ fails to create the object.
124 	 */
125 	public this(Value value)
126 	{
127 		auto __p = gdk_content_provider_new_for_value((value is null) ? null : value.getValueStruct());
128 
129 		if(__p is null)
130 		{
131 			throw new ConstructionException("null returned by new_for_value");
132 		}
133 
134 		this(cast(GdkContentProvider*) __p, true);
135 	}
136 
137 	/**
138 	 * Creates a content provider that represents all the given @providers.
139 	 *
140 	 * Whenever data needs to be written, the union provider will try the given
141 	 * @providers in the given order and the first one supporting a format will
142 	 * be chosen to provide it.
143 	 *
144 	 * This allows an easy way to support providing data in different formats.
145 	 * For example, an image may be provided by its file and by the image
146 	 * contents with a call such as
147 	 * ```c
148 	 * gdk_content_provider_new_union ((GdkContentProvider *[2]) {
149 	 * gdk_content_provider_new_typed (G_TYPE_FILE, file),
150 	 * gdk_content_provider_new_typed (G_TYPE_TEXTURE, texture)
151 	 * }, 2);
152 	 * ```
153 	 *
154 	 * Params:
155 	 *     providers = The `GdkContentProvider`s to present the union of
156 	 *
157 	 * Returns: a new `GdkContentProvider`
158 	 *
159 	 * Throws: ConstructionException GTK+ fails to create the object.
160 	 */
161 	public this(ContentProvider[] providers)
162 	{
163 		GdkContentProvider*[] providersArray = new GdkContentProvider*[providers.length];
164 		for ( int i = 0; i < providers.length; i++ )
165 		{
166 			providersArray[i] = providers[i].getContentProviderStruct();
167 		}
168 
169 		auto __p = gdk_content_provider_new_union(providersArray.ptr, cast(size_t)providers.length);
170 
171 		if(__p is null)
172 		{
173 			throw new ConstructionException("null returned by new_union");
174 		}
175 
176 		this(cast(GdkContentProvider*) __p, true);
177 	}
178 
179 	/**
180 	 * Emits the ::content-changed signal.
181 	 */
182 	public void contentChanged()
183 	{
184 		gdk_content_provider_content_changed(gdkContentProvider);
185 	}
186 
187 	/**
188 	 * Gets the contents of @provider stored in @value.
189 	 *
190 	 * The @value will have been initialized to the `GType` the value should be
191 	 * provided in. This given `GType` does not need to be listed in the formats
192 	 * returned by [method@Gdk.ContentProvider.ref_formats]. However, if the
193 	 * given `GType` is not supported, this operation can fail and
194 	 * `G_IO_ERROR_NOT_SUPPORTED` will be reported.
195 	 *
196 	 * Params:
197 	 *     value = the `GValue` to fill
198 	 *
199 	 * Returns: %TRUE if the value was set successfully. Otherwise
200 	 *     @error will be set to describe the failure.
201 	 *
202 	 * Throws: GException on failure.
203 	 */
204 	public bool getValue(out Value value)
205 	{
206 		GValue* outvalue = sliceNew!GValue();
207 		GError* err = null;
208 
209 		auto __p = gdk_content_provider_get_value(gdkContentProvider, outvalue, &err) != 0;
210 
211 		if (err !is null)
212 		{
213 			throw new GException( new ErrorG(err) );
214 		}
215 
216 		value = ObjectG.getDObject!(Value)(outvalue, true);
217 
218 		return __p;
219 	}
220 
221 	/**
222 	 * Gets the formats that the provider can provide its current contents in.
223 	 *
224 	 * Returns: The formats of the provider
225 	 */
226 	public ContentFormats refFormats()
227 	{
228 		auto __p = gdk_content_provider_ref_formats(gdkContentProvider);
229 
230 		if(__p is null)
231 		{
232 			return null;
233 		}
234 
235 		return ObjectG.getDObject!(ContentFormats)(cast(GdkContentFormats*) __p, true);
236 	}
237 
238 	/**
239 	 * Gets the formats that the provider suggests other applications to store
240 	 * the data in.
241 	 *
242 	 * An example of such an application would be a clipboard manager.
243 	 *
244 	 * This can be assumed to be a subset of [method@Gdk.ContentProvider.ref_formats].
245 	 *
246 	 * Returns: The storable formats of the provider
247 	 */
248 	public ContentFormats refStorableFormats()
249 	{
250 		auto __p = gdk_content_provider_ref_storable_formats(gdkContentProvider);
251 
252 		if(__p is null)
253 		{
254 			return null;
255 		}
256 
257 		return ObjectG.getDObject!(ContentFormats)(cast(GdkContentFormats*) __p, true);
258 	}
259 
260 	/**
261 	 * Asynchronously writes the contents of @provider to @stream in the given
262 	 * @mime_type.
263 	 *
264 	 * When the operation is finished @callback will be called. You must then call
265 	 * [method@Gdk.ContentProvider.write_mime_type_finish] to get the result
266 	 * of the operation.
267 	 *
268 	 * The given mime type does not need to be listed in the formats returned by
269 	 * [method@Gdk.ContentProvider.ref_formats]. However, if the given `GType` is
270 	 * not supported, `G_IO_ERROR_NOT_SUPPORTED` will be reported.
271 	 *
272 	 * The given @stream will not be closed.
273 	 *
274 	 * Params:
275 	 *     mimeType = the mime type to provide the data in
276 	 *     stream = the `GOutputStream` to write to
277 	 *     ioPriority = I/O priority of the request.
278 	 *     cancellable = optional `GCancellable` object, %NULL to ignore.
279 	 *     callback = callback to call when the request is satisfied
280 	 *     userData = the data to pass to callback function
281 	 */
282 	public void writeMimeTypeAsync(string mimeType, OutputStream stream, int ioPriority, Cancellable cancellable, GAsyncReadyCallback callback, void* userData)
283 	{
284 		gdk_content_provider_write_mime_type_async(gdkContentProvider, Str.toStringz(mimeType), (stream is null) ? null : stream.getOutputStreamStruct(), ioPriority, (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData);
285 	}
286 
287 	/**
288 	 * Finishes an asynchronous write operation.
289 	 *
290 	 * See [method@Gdk.ContentProvider.write_mime_type_async].
291 	 *
292 	 * Params:
293 	 *     result = a `GAsyncResult`
294 	 *
295 	 * Returns: %TRUE if the operation was completed successfully. Otherwise
296 	 *     @error will be set to describe the failure.
297 	 *
298 	 * Throws: GException on failure.
299 	 */
300 	public bool writeMimeTypeFinish(AsyncResultIF result)
301 	{
302 		GError* err = null;
303 
304 		auto __p = gdk_content_provider_write_mime_type_finish(gdkContentProvider, (result is null) ? null : result.getAsyncResultStruct(), &err) != 0;
305 
306 		if (err !is null)
307 		{
308 			throw new GException( new ErrorG(err) );
309 		}
310 
311 		return __p;
312 	}
313 
314 	/**
315 	 * Emitted whenever the content provided by this provider has changed.
316 	 */
317 	gulong addOnContentChanged(void delegate(ContentProvider) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
318 	{
319 		return Signals.connect(this, "content-changed", dlg, connectFlags ^ ConnectFlags.SWAPPED);
320 	}
321 }